//消息发送出去
/* console.log("content.js 开始向background发消息")
let NewTableMain =  $(".NewTableMain");
console.log(NewTableMain);
let data = chrome.runtime.sendMessage({NewTableMain:NewTableMain});
console.log(data) 

*/

/* 接受别人来的请求 */
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse){
        console.log(request);
        if(request.xls_data){
            console.log(request);
            let NewTableMain =  $(".NewTableMain");
            console.log(NewTableMain);


            // 参数是文件位置
            let xls_data = request.xls_data
            //解析资产负债表数据 key序号  value期末值
            dataMap = new Map
            for(let i =5;i<xls_data.length;i++){
                let data =  xls_data[i] //当前行数据
                let key1 =  data[1] 
                let value1 =  data[2]
                let key2 =  data[5]
                let value2 =  data[6]
                if(value1 == ''){
                    value1 = 0.00
                }
                if(value2 == ''){
                    value2 = 0.00
                }
                dataMap.set(key1,value1)
                dataMap.set(key2,value2)
            }

            dataMap.forEach(function(value, key) {
                    console.log('key='+key);
                    console.log('value='+value);
                }
            )

            NewTableMain = $("div.NewTableMain")[0]  //获取主table
            table = NewTableMain.childNodes[1]  //获取表格table标签
            tbody = table.childNodes[1]  //获取表格 tbody 标签
            trList = tbody.children  //表格tr标签
            for(let i = 2 ; i<trList.length;i++){ 
                //资产负债表 从第三行开始时需要填写的数据
                datastr = trList[i];
                tdList = datastr.children; //td标签
                //获取要填写数据的行次表值
                value1 = dataMap.get(tdList[1].textContent)
                tdList[2].childNodes[0].value=value1
                value2 = dataMap.get(tdList[5].textContent)
                tdList[6].childNodes[0].value=value2

            }


        }
        sendResponse("content.js回复内容");
        /* 图标徽标 */
        chrome.action.setBadgeText({ text: '回写' });
    }
    
)

